var app = getApp();
Page({
    data:{
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        Popanimation : true,
        swipheight : 320,
        isIPX : app.globalData.isIPX,
        nodes : '',
    },
    onShow: function () {
        wx.hideTabBar();
    },
    onLoad:function(options){
        var self = this;
        
        var gid = options.id ? options.id : '';
        self.setData({
            options : options,
            zfid : options.zfid ? options.zfid : '',
        });

        app.com.auth(function(user){
        	self.getData(gid);
        },self);
      
    },
    getData(gid){
    	var self = this;
        var postdata = {
            gid : gid,
            op : 'goodsinfo',
            zfid : self.data.zfid,
        };

        app.com.http('jifen','POST',postdata,0,false,'','',function(res){
            if (!res.data.errno  && res.data) {

                self.setData({
                	Pagetheme : res.data.data.params.style,
                    good : res.data.data.good,
                    rule : res.data.data.good.rulearray ? res.data.data.good.rulearray.rule : {},
                    rulemap : res.data.data.good.rulearray ? res.data.data.good.rulearray.rulemap : {},
                    params : res.data.data.params,
                    userinfo : res.data.data.userinfo,
                    address : res.data.data.address,
                    sharedata : res.data.data.sharedata,
                    inited : true,
                },function(){


                });
                
                if( res.data.data.good.content ){
                    var octoparse = require('../resource/wxParse/html2json2.js');
                    var transData = octoparse.parse(res.data.data.good.content);                   
                    self.setData({
                        nodes : transData,
                    })
                }

                wx.setNavigationBarTitle({
                    title:  res.data.data.good.name,
                });

            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
    	this.setData({
    		SharePopupshow : false,
    		hideInfoNone : false,
    	})

        this.data.isdown = true;
        this.onLoad( this.data.options );
        this.data.isdown = false;

        // 关闭下拉刷新
        wx.stopPullDownRefresh();
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    changeit(e){
        var self = this;
        if( self.data.userinfo.jifen*1 < self.data.good.need*1 ){
            app.com.toast('你的'+self.data.params.jfbname+'不足');return false;
        }

        self.setData({
            confirm : true
        });

    },
    confirmbuy : function(){
        var self = this;

        if( !this.data.address ){
            app.com.toast('请先选择地址');return false;
        }
        if( !this.data.address.name ){
            app.com.toast('请先选择地址');return false;
        }
        if( !this.data.address.tel ){
            app.com.toast('请先选择地址');return false;
        }

        if( self.data.good.isrule == 1 ){
            for (var i = 0; i < self.data.rule.length; i++) {
                if( !self.data.rule[i].actitem || typeof self.data.rule[i].actitem == 'undefined' ){
                    app.com.toast('请选择规格');return false;
                }
            }
        }

        var postdata = {
            op : 'changegoods',
            gid : self.data.good.id,
            name : self.data.address.name,
            tel : self.data.address.tel,
            map : self.data.actmap,
            address : self.data.address.region ? self.data.address.region : '',
            street : self.data.address.street ? self.data.address.street : '',
        }
        app.com.confirm('确定兑换吗？',function(){
            app.com.http('jifen','POST',postdata,0,true,'','',function(res){
                if (!res.data.errno  && res.data) {

                    if( res.data.data.paySign ){
                        wx.requestPayment({
							'appId' : res.data.data.appId,
                            'timeStamp': res.data.data.timeStamp,
                            'nonceStr': res.data.data.nonceStr,
                            'package': res.data.data.package,
                            'signType': 'MD5',
                            'paySign': res.data.data.paySign,
                            'success': function (payres) {
                                if( payres.errMsg == 'requestPayment:ok' ){
                                    self.setData({
                                        confirm : false,
                                        'good.stock' : self.data.good.stock*1 - 1,
                                        'good.taked' : self.data.good.taked*1 + 1,
                                    });
                                    app.com.toast('兑换完成，请等待系统处理');
                                }
                            },
                            'fail': function (payres) {
                                console.log(payres);
                            }
                        });
                    }else{
                        self.setData({
                            confirm : false,
                            'good.stock' : self.data.good.stock*1 - 1,
                            'good.taked' : self.data.good.taked*1 + 1,
                        });
                        app.com.toast(res.data.message);
                    }
                }else{
                    app.com.alert(res.data.message);
                }
            });
        });
    },
    changeRule( e ){
        for (var i = 0; i < this.data.rule.length; i++) {
            if( this.data.rule[i].pro.id == e.currentTarget.dataset.oid ){
                var target = 'rule['+i+'].actitem';
                this.setData({
                    [target] : e.currentTarget.dataset.tid,
                });

                var selectarr = [];
                
                for (var j = 0; j < this.data.rule.length; j++) {
                    if( this.data.rule[j].actitem ) {
                        selectarr.push( this.data.rule[j].actitem );
                    }
                }
                if( selectarr.length < this.data.rule.length ) return false;
                
                for (var j = 0; j < this.data.rulemap.length; j++) {
                    var thisstr = ( this.data.rulemap[j].id + '' ).split(":");
                    
                    if( thisstr.sort().toString() == selectarr.sort().toString() ){
                        
                        this.setData({
                            'good.stock' : this.data.rulemap[j].stock,
                            'good.fee' : this.data.rulemap[j].nowprice,
                            'good.need' : this.data.rulemap[j].jifen,
                            'actmap' : this.data.rulemap[j].id
                        });
                        
                        return false;
                    }
                }
                return false;
            }
        }
    },
    togglebuy : function(){
        this.setData({
            confirm : false
        });
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    switchTab(e){
        wx.switchTab({
            url: e.currentTarget.dataset.url
        });
    },
    back(){
        wx.navigateBack();
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    preimg : function( e ){
        wx.previewImage({
            urls: e.currentTarget.dataset.img
        });
    },
    FormIdsubmit : function(e){
        if( app.globalData.formarr.length <= 20 ) app.globalData.formarr.push(e.detail.formId);
        //app.com.alert(e.detail.formId);
    },
    imageLoad : function(e){
       var self = this;
        //获取图片真实宽度  
        var imgwidth = e.detail.width,
        imgheight = e.detail.height;
        //宽高比
        var ratio = 1; 
        wx.getSystemInfo({ 
            success: function (res) {
                ratio = res.windowWidth / imgwidth;
            }
        });
        var viewHeight = imgheight*ratio;
        self.setData({
            swipheight: viewHeight,
        });
    },
    updateUserInfo : function(e){
        var self = this;
        app.com.getUserInfo(function(res){
            self.setData({
                showuserbtn:false,
            });
        },false,e.detail);
    },
    hideauth(){
        this.setData({
            showuserbtn:false,
        });
    },
})